package util.http

import scala.util.Try

object Retry {
  
  def apply[R](tries: => R, catches: Throwable => Unit = { e => e.printStackTrace() }, finalDo: => Unit = {})(isOk: Option[R] => Boolean = { o: Option[R] => None != o }) = {
    var r: Option[R] = None
    while (!isOk(r))
      try {
        r = Option(tries)
      } catch {
        case e: Throwable => catches(e)
      } finally {
        finalDo
      }

    r.get
  }
  
  def apply[R](tries: => R)(isOk: Try[R] => Boolean = { o: Try[R] => o.isSuccess }) = {
    var r: Try[R] = null
    while (!isOk(r) || r == null)
      r = Try {
        (tries)
      }
    r
  }
}